const state = {
    cachedViews: [],
    visitedViews: []
}

const mutations = {
    ADD_VISITED_VIEW: (state, view) => {
        if (state.visitedViews.some(v => v.path === view.path)) return
        state.visitedViews.push(
            Object.assign({}, view, {
                title: view.meta.title || 'no-name'
            })
        )
    },

    ADD_CACHED_VIEW: (state, view) => {
        if (state.cachedViews.includes(view.name)) return
        if (!view.meta.noCache) {
            state.cachedViews.push(view.name)
        }
    }
}

const actions = {
    add_view: ({ dispatch }, view) => {
        // console.log(view)
        dispatch('add_visited_view', view)
        dispatch('add_cached_view', view)
    },
    add_visited_view: ({ commit }, view) => {
        commit('ADD_VISITED_VIEW', view)
    },
    add_cached_view: ({ commit }, view) => {
        commit('ADD_CACHED_VIEW', view)
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}